﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SERIX.Classes;

namespace SERIX
{
    public partial class ProgramaMunicipios : Seguranca
    {      

        protected void Page_Load(object sender, EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
            {
                int anoLiberado = CPlanejamento.AnoPlanejamentoCorrente(this.usuAut.Login);
                int ano = DateTime.Now.Year;
                for (int i = ano - 4; i <= ano + 2; i++)
                {
                    string valor = i.ToString();
                    ddlAno.Items.Add(new ListItem(valor, valor, true));
                }
                ddlAno.SelectedValue = anoLiberado.ToString();
             }
        }

        protected void odsProgramas_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (string.IsNullOrEmpty(ddlProgramas.SelectedValue) || 
                string.IsNullOrEmpty(ddlAno.SelectedValue))
            {
                e.Cancel = true;
                btnConfirmar.Visible = false;
            }
            else
                btnConfirmar.Visible = true;

            e.InputParameters["idt_prog"] = ddlProgramas.SelectedValue;
            lblmsg.Visible = false;
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal programa = (decimal)(((DataRowView)e.Row.DataItem))["programa"];
                if (programa == 1)
                    ((CheckBox)e.Row.FindControl("cbxProgramado")).Checked = true;
            }
        }

        protected void btnConfirmar_Click(object sender, EventArgs e)
        {
            int idt_prog = Convert.ToInt32(ddlProgramas.SelectedValue);
            int pms_Ano = Convert.ToInt32(ddlAno.SelectedValue);
            List<int> idt_Muns = new List<int>();
            foreach (GridViewRow row in gridView.Rows)
            {
                if (((CheckBox)row.FindControl("cbxProgramado")).Checked)
                    idt_Muns.Add(Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_mun"]));
            }
            
            CProgramasMunicipios.PermissoesEmLote(idt_prog, idt_Muns, pms_Ano, UsuAut.Login);
            lblmsg.Text = "Operação realizada com sucesso";
            lblmsg.Visible = true;
        }

        protected void cbxMuniciopiosHeader_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            bool check = false;
            if (cbx.Checked)
                check = true;
            foreach (GridViewRow row in gridView.Rows)
            {
                ((CheckBox)row.FindControl("cbxProgramado")).Checked = check;
            }
        }

        protected void ddlAno_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView.DataBind();
            int ANO_AIS = Convert.ToInt32(ddlAno.SelectedValue);
            if (!CPlanejamento.AnoLiberadoPlanejamento(ANO_AIS, UsuAut.Login))
            {
                gridView.Enabled = false;
                lblmsg.Visible = true;
                lblmsg.Text = "Modificação do Planejamento bloqueado para esse ano.";
            }
            else
            {
                gridView.Enabled = true;
                lblmsg.Visible = false;
            }
        }

        protected void ddlProgramas_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView.DataBind();
        }
    }
}
